package com.zksk.portal.service.manual.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksk.common.core.constant.PortalConstants;
import com.zksk.common.core.domain.R;
import com.zksk.mbg.admin.domain.Manual;
import com.zksk.mbg.admin.mapper.ManualMapper;
import com.zksk.admin.api.domain.portal.dto.ManualDto;
import com.zksk.admin.api.domain.portal.vo.ManualVo;
import com.zksk.portal.service.manual.ManualService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description:
 * @author: xiebo
 * @create: 2022-12-27 10:06
 **/
@Service
public class ManualServiceImpl extends ServiceImpl<ManualMapper, Manual> implements ManualService
{

    @Autowired
    private ManualMapper manualMapper;

    /**
     * 分页条件查询操作指南
     *
     * @param manualDto
     * @return
     */
    @Override
    public R<Page<ManualVo>> selectList(ManualDto manualDto)
    {

        Page<ManualVo> voPage = new Page<>();
        Page<Manual> manualPage = manualMapper.selectPage
                (
                        //分页
                        new Page<>(manualDto.getPageNum(), manualDto.getPageSize()),
                        //条件构造
                        new LambdaQueryWrapper<Manual>()
                                .eq(ObjectUtil.isNotEmpty(manualDto.getType()), Manual::getType, manualDto.getType())
                );
        //实体转Vo
        BeanUtil.copyProperties(manualPage, voPage);

        return R.ok(voPage);
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public R<ManualVo> selectById(Long id)
    {

        ManualVo manualVo = new ManualVo();
        //查询数据
        Manual manual = manualMapper.selectById(id);
        //查看次数 + 1
        if (ObjectUtil.isEmpty(manual.getLookNum()))
        {
            manual.setLookNum(PortalConstants.LOOK_NUM_1);
        }
        else
        {
            manual.setLookNum(manual.getLookNum() + 1);
        }
        manualMapper.updateById(manual);
        //实体转Vo
        BeanUtil.copyProperties(manual, manualVo);

        return R.ok(manualVo);
    }

}
